package com.violet.myorm.core.sql;

import java.util.List;

public abstract class Sql {

    protected final StringBuilder sql = new StringBuilder();

    public String getSql() { return sql.append(";").toString(); }

    protected void setQueryFields(List<String> fieldsNames) {
        if (fieldsNames == null || fieldsNames.isEmpty()) throw new RuntimeException("必须提供查询字段!");
        for (String field : fieldsNames) sql.append(field).append(",");
        sql.deleteCharAt(sql.length() - 1);
    }

    protected void setUpdateFields(List<String> fields) {
        if (fields == null || fields.isEmpty()) throw new RuntimeException("必须提供修改字段!");
        for (int i = 0; i < fields.size(); i++) {
            sql.append(fields.get(i)).append(" = ?");
            if (i < fields.size() - 1) sql.append(", ");
        }
    }

    protected Object setLimit(int offset, int size) {
        sql.append("LIMIT ").append(offset).append(",").append(size);
        return this;
    }

    protected Object setWhere(List<String> where) {
        if (where == null || where.isEmpty()) return this;

        sql.append(" WHERE ");
        for (int i = 0; i < where.size(); i++) {
            sql.append(where.get(i));
            if (i < where.size() - 1) sql.append(" ");
        }
        return this;
    }
}
